package daybook.io.formatter;

import java.util.Objects;

public enum  Format {
    STRING(".txt"), JSON(".json");

    private final String fileExtension;

    Format(String fileExtension) {
        this.fileExtension = Objects.requireNonNull(fileExtension);
    }

    public String getFileExtension() {
        return fileExtension;
    }

    public String appendFileExtension(String filename, boolean force) {
        if (force) {
            return filename + getFileExtension();
        } else {
            final int indexOfPeriod = filename.lastIndexOf('.');
            if (indexOfPeriod != -1 && indexOfPeriod != filename.length() - 1) {
                return filename;
            } else {
                return filename + getFileExtension();
            }
        }
    }
}
